package com.atguigu.blog.controller;

import com.atguigu.blog.pojo.User;
import com.atguigu.blog.service.UserService;
import com.atguigu.blog.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @ClassName UserController
 * @Description TODO
 * @Author 孤妤
 * @Date 2023/11/24 16:51
 */
@RestController
@RequestMapping("user")
@CrossOrigin
@Tag(name = "用户模块")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    @Operation(summary = "登陆")
    @Parameters(@Parameter(name = "user", description = "用户对象", required = true))
    public Result<Map<String, Object>> showByUsername(@RequestBody User user) {
        Map<String, Object> map = userService.showByUsername(user);
        if (map != null) {
            return Result.ok(map);
        }
        return Result.fail();
    }
    @GetMapping("{uid}")
    @Operation(summary = "查看用户详细信息")
    @Parameters(@Parameter(name = "uid",description = "用户的的uid",required = true))
    public Result<User> showByUid(@PathVariable Integer uid){
        User user = userService.showByUid(uid);
        return Result.ok(user);
    }
    @PutMapping
    @Operation(summary = "更改用户详细信息")
    @Parameters(@Parameter(name = "user",description = "用户对象",required = true))
    public Result change(@RequestBody User user){
        Integer result = userService.change(user);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
}